/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.web.servlet.serveur;

import com.supinfo.youfood.entity.Commande;
import com.supinfo.youfood.entity.Commande_Menu;
import com.supinfo.youfood.entity.Commande_Produit;
import com.supinfo.youfood.entity.Employe;
import com.supinfo.youfood.service.CommandeService;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Michael
 */
@WebServlet(name = "DetailsCommandeServlet", urlPatterns = {"/serveur/DetailsCommande"})
public class DetailsCommandeServlet extends HttpServlet {

    @EJB
    private CommandeService commandeService;
    
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Long id_commande = Long.valueOf(request.getParameter("id_commande"));
        Commande currentCommande = commandeService.findCommandeById(id_commande);
        for(Commande_Menu cm : currentCommande.getList_menus()){
            int etatM = cm.getEtat();
            request.setAttribute("etatM", etatM);
        }
        for(Commande_Produit cp : currentCommande.getList_produits()){
            int etatP = cp.getEtat();
            request.setAttribute("etatP", etatP);
        }
        
        request.setAttribute("currentCommande", currentCommande);
        request.getRequestDispatcher("/jsp/serveur/details.jsp").forward(request, response);
    }

    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Long id_commande = Long.valueOf(request.getParameter("ID_Commande"));
        Commande currentCommande = commandeService.findCommandeById(id_commande);
        currentCommande.setServeur((Employe)request.getSession(true).getAttribute("currentEmp"));
        commandeService.updateCommande(currentCommande);
        
        response.sendRedirect("accueil");
    }
}
